var JSObject = {

    selected_row_id:null,
    objGrid:null,

    init:function () {
        var self = this;

        self.objGrid = $('#flex').flexigrid({
            url:$('#table-url').val(),
            reload_after_add:true,
            dataType:'json',
            colModel:[
                {display:'ID', name:'id', width:20, sortable:false, align:'left', hide:true},
                {display:'Customer Name', name:'name', width:120, sortable:true, align:'left', hide:false, editable:{form:'text', validate:'empty', defval:''}},
                {display:'Telephone', name:'telephone', width:100, sortable:true, align:'left', hide:false, editable:{form:'text', validate:'empty,onlyNumber', defval:'',placeholder:'233244444444'}},
                {display:'Email', name:'email', width:150, sortable:true, align:'left', hide:false, editable:{form:'text', validate:'', defval:''}},
                {display:'Address', name:'address', width:150, sortable:true, align:'left', hide:false, editable:{form:'text', validate:'', defval:''}},
                {display:'Date Of Birth', name:'dob', width:100, sortable:true, align:'left', hide:false, editable:{form:'text', validate:'', defval:'', bclass:'datepicker' ,placeholder:'yyyy-mm-dd'}},
                {display:'Birthday Wish Message', name:'dob_msg', width:270, sortable:true, align:'left', hide:false, editable:{form:'text', validate:'', defval:''}}
            ],
            formFields:[
                {type:'buttom', name:'New', bclass:'add', onpress:self.handleGridEvent},
                {separator:true},
                {type:'buttom', name:'Save', bclass:'save', onpress:self.handleGridEvent},
                {separator:true},
                {type:'buttom', name:'Edit', bclass:'edit', onpress:self.handleGridEvent},
                {separator:true},
                {type:'buttom', name:'Cancel', bclass:'cancel', onpress:self.handleGridEvent}
                /*{separator:true},
                {type:'buttom', name:'Export All', bclass:'export', onpress:self.handleGridEvent}*/
                //{type:'buttom', name:'Delete', bclass:'delete', onpress:self.handleGridEvent},
                //{separator:true}
            ],
            searchitems:[
                {display:'Customer Name', name:'name', isdefault:true},
                {display:'Telephone', name:'telephone'},
                {display:'Email', name:'email'},
                {display:'Address', name:'address'}
             ],
            checkboxSelection:true,
            editablegrid:{
                use:true,
                url:$('#table-editable-url').val()
            },
            columnControl:false,
            sortname:"id",
            sortorder:"desc",
            usepager:true,
            useRp:true,
            rp:15,
            showTableToggleBtn:false,
            height:300,
            subgrid:{
                use:false
            }
        });

        $('input.datepicker').live('focus', function(){
            if (false == $(this).hasClass('hasDatepicker')) {
                $(this).datepicker({
                    inline: true,
                    changeMonth: true,
                    changeYear: true
                });
                $(this).datepicker( "option", "dateFormat", 'yy-mm-dd' );
            }
        });

        $("#birthday-modal-btn").click(function(){
            $('#birthday_modal').modal({
                backdrop: 'static',
                show: true,
                keyboard: true
            });
        });

        $("#save_default_bdmsg_btn").click(function(){
            self.saveSMSMessage($(this).attr('href'));
        });
    },


    saveSMSMessage:function(url){
        var self = this;
        var message = $("#form-sms #message").val();
        var message = message.trim();
        if(message.length == 0 ){
            jLib.serverError("Please enter a message.");
            return;
        }

        var query = $("#birthday_modal #form-sms").serialize();
        $.ajax({
            type: 'post',
            url: url,
            data: query,
            dataType: 'json',
            success: function(response) {
                var txt = '';
                if (typeof response.msg == 'object') {
                    for (var megTxt in response.msg) {
                        txt += response.msg[megTxt] + '<br />';
                    }
                }
                else {
                    txt = response.msg
                }
                if(response.code == 0){
                    jLib.serverSuccess(txt);
                    $("#close_sms_btn").click();
                }
                else{
                    jLib.serverError(txt);
                }
            },
            error: function() {
                jLib.serverError();
            }
        });
    },

    handleGridEvent:function (com, grid, json) {
        if (com == 'New') {
            JSObject.objGrid.flexBeginAdd();
        }
        else if (com == 'Edit') {
            var r = FlexObject.rowSelectedCheck(JSObject.objGrid,grid,1);
            if(r){
                var row = FlexObject.getSelectedRows(grid);
                JSObject.objGrid.flexBeginEdit(row[0]);
            }
        }
        else if (com == 'Save') {
            JSObject.objGrid.flexSaveChanges();
        }
        else if (com == 'Cancel') {
            JSObject.objGrid.flexCancel();
        }
        else if (com == 'Delete') {
            var r = FlexObject.rowSelectedCheck(JSObject.objGrid,grid);
            if(r){
                JSObject.delete_(grid);
            }
        }
        else if (com == 'Export All') {
            var url = $("#export_url").val();
            window.open(url, "PrintExportWindow", "menubar=yes, width=600, height=500,location=no,status=no,scrollbars=yes,resizable=yes");
        }
    },

    delete_:function (grid) {
        var self = this;
        var url = $('#delete_url').val();
        jLib.do_delete(url, grid,function(response){
            if(response.code == 0){
                FlexObject.removeGridRows(grid);
                self.objGrid.flexNotify('Status', response.msg, 'success');
            }
            else if(response.code == 1){
                self.objGrid.flexNotify('Status', response.msg, 'error');
            }
        });
    }
};

/* when the page is loaded */
$(document).ready(function () {
    JSObject.init();
});